﻿using AntdUI;
using SqlSugar.Extensions;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TestPressure
{
    public partial class ProjectAddForm : BaseForm
    {
        #region 属性

        private Project obj = new Project();

        public TemplateItem templateObj;

        #endregion

        #region 自定义方法

        /// <summary>重复检测
        /// 
        /// </summary>
        /// <returns></returns>
        private bool CheckRepeat()
        {
            Dictionary<string, Object> where = new Dictionary<string, Object>();
            where["project_number"] = ProjectNumbernput.Text;
            List<Project> list = SqlSugarHelper.Inquireable<Project>(where);
            int count = list.Count;
            if (count > 0)
            {
                string msg = string.Format("【{0}】工程编号已存在", where["project_number"]);
                Error(msg, this);
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>格式化判断方式
        /// 
        /// </summary>
        /// <param name="estimate"></param>
        /// <returns></returns>
        private int FormatEstimate(string str)
        {
            if (str == "固定数值")
            {
                return 0;
            }
            else
            {
                return 1;
            }
        }

        /// <summary>加载模板信息
        /// 
        /// </summary>
        public void LoadTemplateInfo()
        {
            if (templateObj is not null)
            {
                bool res =  CheckTemplateExist(templateObj.template_number);
                if (res)
                {
                    DesignPressureInput.Text = templateObj.design_pressure.ToString();
                    if (templateObj.pipe_position is not null)
                    {
                        if (templateObj.pipe_position.Contains("站内/集输"))
                        {
                            TestTypeSelect.Text = "站内/集输";
                        }
                        else if (templateObj.pipe_position.Contains("站内"))
                        {
                            TestTypeSelect.Text = "站内";
                        }
                        else if (templateObj.pipe_position.Contains("集输"))
                        {
                            TestTypeSelect.Text = "集输";
                        }
                    }
                }
                
            }
            else
            {
                string msg = string.Format("模板不存在");
                Error(msg,this);
            }
            this.DesignPressureInput.Enabled = true;
        }

        /// <summary>判断设备是否存在
        /// 
        /// </summary>
        /// <param name="number"></param>
        private bool CheckTemplateExist(int number)
        {
            Dictionary<string, Object> where = new Dictionary<string, Object>();
            where["template_number"] = number;
            List<Template> list = SqlSugarHelper.Inquireable<Template>(where);
            if (list.Count == 0)
            {
                string msg = string.Format("【{0}】编号模板不存在", number);
                Error(msg,this);
                return false;
            }
            else
            { 
                return true;
            }
        }
        #endregion

        #region 系统方法
        public ProjectAddForm()
        {
            InitializeComponent();
            Header.Text = "添加工程";
        }

        private void ProjectAddForm_Load(object sender, EventArgs e)
        {

        }
        #endregion

        #region 响应事件
        private void ConfirmButton_Click(object sender, EventArgs e)
        {
            if (ProjectNumbernput.Text.Length == 0)
            {
                Error("请输入工程编号", this);
                ProjectNumbernput.Focus();
                return;
            }
            if (ProjectNameInput.Text.Length == 0)
            {
                Error("请输入工程名称", this);
                ProjectNameInput.Focus();
                return;
            }
            if (PipeLengthInput.Text.Length == 0)
            {
                Error("请输入管道长度", this);
                PipeLengthInput.Focus();
                return;
            }
            
            if (templateObj.template_number <= 0)
            {
                Error("请选择试压模板", this);
                return;
            }

            bool repeatRes = CheckRepeat();
            if (repeatRes)
            {
                return;
            }

            obj.project_number = ProjectNumbernput.Text;
            obj.project_name = ProjectNameInput.Text;
            obj.stake_mark = StakeMarkInput.Text;
            obj.pipe_specification = PipeSpecificationInput.Text;
            obj.pipe_length = float.Parse(PipeLengthInput.Text);
            obj.medium = MediumInput.Text;
            obj.design_pressure = float.Parse(DesignPressureInput.Text);
            obj.pipe_nature = PipeNatureSelect.Text;
            obj.test_type = TestTypeSelect.Text;
            obj.company = CompanyInput.Text;
            obj.manometer_precision = ManometerPrecisionSelect.Text;
            obj.manometer_range = ManometerRangeSelect.Text;
            obj.template_id = templateObj.template_number;

            List<Project> list = new List<Project>();
            list.Add(obj);
            int res = SqlSugarHelper.Insertable(list);
            if (res > 0)
            {
                if (this.Owner is not null)
                {
                    ProjectManageForm frm = (ProjectManageForm)Owner;
                    frm.LoadList();
                }
                this.Close();
            }
            else
            {
                Error("添加工程失败", this);
            }
        }
        private void DesignPressureInput_Click(object sender, EventArgs e)
        {
            DesignPressureInput.Enabled = false;
            TemplateSelectForm frm = new TemplateSelectForm();
            frm.Owner = this;
            frm.ShowDialog();
        }
        #endregion
    }
}
